<?php

namespace app\api\controller;

use api\Model\AuthRule;
use app\api\model\Common as _common;
use think\Controller;
use think\Request;

//
// +--------------------------------------+
// |PHP version 5.6
// +--------------------------------------+
// |公共接口
// +--------------------------------------+
// |2018/01/10
// +--------------------------------------+
// |罗彪
// +--------------------------------------+
// |知果汇
// +--------------------------------------+
class Common extends Controller
{


//    protected function _initialize()
//    {

//
//        $auth = new \Auth\Auth();
//        $request = Request::instance();
//        $name = $request->module() . '/' . $request->controller() . '/' . $request->action();
//        $uid = 1;
//        if ($uid == 1) {
//
//        } else if (!$auth->check($name, $uid)) {
//            /* return array('status'=>'error','msg'=>'有权限！');*/
//            $this->_jsonp('1000', '您没有权限', null);
//        } else {
//            return false;
//        }
//    }

    public function menu()
    {
        $userinfo = cookie('username');
        $user = base64_decode($userinfo);
        $id = explode(',', $user);
        if (!isset($id[1])) {
            $this->_jsonp('1000', '您还未登录', null);
        }
        $uid = $id[1];
        $user = db('user')->where('uid', $uid)->find();
        if ($user['r_id'] != 1) {
//            $menus = db('menu')->where('pid', 0)->order('id asc')->select();
////            dump($menus);exit;
//            $auth = new \auth\Auth();
//
//            foreach ($menus as $key => $value) {
////                dump($menus);exit;
//                if (!$auth->check($value['label'], $uid)) {
//                    unset($menus[$key]);
//                    continue;
//                }
//                $id = $menus[$key]['id'];
//                $menus[$key]['children'] = db('menu')->where("pid=$id")->order('id asc')->select();
//                foreach ($menus[$key]['children'] as $k => $v) {
//                    if (!$auth->check($v['label'], $uid)) {
//                        unset($menus[$key]['children'][$k]);
//                    }
//                }
//            }
            $role = db('role')->where('id', $user['r_id'])->find();
            $menus['id'] = array('in', $role['auth']);
            $menus = db('menu')->where($menus)->select();
        } else {
//            $menus = db('menu')->where('pid', 0)->order('id asc')->select();
//            foreach ($menus as $key => $value) {
//                $id = $menus[$key]['id'];
//                $menus[$key]['children'] = db('menu')->where("pid=$id")->order('id asc')->select();
//            }
            $menus = db('menu')->select();
        }
        $this->_jsonp('200', '侧边栏查询成功', $menus);
    }

    /*
     * 菜单信息接口
     * @date:2018/01/10
     * @author:罗彪
     * @parse:
            $Log:实例化日志接口
     * @return:
            $result:游客日志列表数据
     */
    public function menu_list()
    {
        $_common = new _common;
        $result = $_common->menu_list();
        return jsonp($result);
    }

    public function rmdirs()
    {
        $_common = new _common;
        $result = $_common->deldir("./Mysql/2018-01-12 10:17:59/");
        var_dump($result);
        exit;
    }

    protected static function _jsonp($code, $message = '', $data)
    {
        if (!is_numeric($code)) {
            return '';
        }
        $data = array(
            'code' => $code,
            'message' => $message,
            'data' => $data
        );
        $callback = $_GET['callback'];
        echo $callback . '(' . json_encode($data) . ')';
        exit;
    }
}
